package com.itheima.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;



import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;



@Component
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private UserService userService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        QueryWrapper<User>queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("user_name",s);
        User user = userService.getOne(queryWrapper);
        System.out.println(user);
        if (user!=null){
            System.out.println("11111111111");
            return new org.springframework.security.core.userdetails.User(s,user.getPassword(),AuthorityUtils.commaSeparatedStringToAuthorityList(user.getUserRole()));
        }
        return null;
    }
}
